<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8"/>
    <meta content="IE=edge" http-equiv="X-UA-Compatible"/>
    <meta content="width=device-width, initial-scale=1.0" name="viewport"/>
    <title>Salvo Demo</title>
</head>
<body class="bg-gradient-to-br from-blue-400 via-teal-400 to-green-500 min-h-screen" id="body">
<div x-data="loginForm()">
    <div class="flex min-h-screen items-center justify-center py-12 px-4 sm:px-6 lg:px-8">
        <div class="w-full max-w-md space-y-6 bg-white bg-opacity-80 p-10 rounded-3xl shadow-xl border border-blue-200">
            <div>
                <h2 class="text-center text-4xl font-extrabold tracking-tight text-blue-900">
                    登录
                </h2>
                <p class="mt-2 text-center text-sm text-gray-600">
                    由 Salvo-Cli 生成，使用前请阅读 README.md
                </p>
            </div>
            <form @submit.prevent="submit" class="mt-8 space-y-6">
                <input name="remember" type="hidden" value="true"/>
                <div class="space-y-4">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1" for="username">用户名</label>
                        <input
                                autocomplete="username"
                                class="block w-full appearance-none rounded-lg border border-gray-300 px-4 py-3 text-gray-900 placeholder-gray-400 focus:border-teal-500 focus:outline-none focus:ring-2 focus:ring-teal-300 sm:text-sm transition"
                                id="username"
                                name="username"
                                placeholder="用户名"
                                required
                                type="text"
                                x-model="username"
                        />
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1" for="password">密码</label>
                        <input
                                autocomplete="current-password"
                                class="block w-full appearance-none rounded-lg border border-gray-300 px-4 py-3 text-gray-900 placeholder-gray-400 focus:border-teal-500 focus:outline-none focus:ring-2 focus:ring-teal-300 sm:text-sm transition"
                                id="password"
                                name="password"
                                placeholder="密码"
                                required
                                type="password"
                                x-model="password"
                        />
                    </div>
                </div>

                <div>
                    <button
                            class="group relative w-full flex justify-center py-3 px-4 border border-transparent text-sm font-medium rounded-lg text-white bg-gradient-to-r from-blue-600 to-green-600 hover:from-blue-700 hover:to-green-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 transition-colors duration-200"
                            type="submit"
                    >
                        登录
                    </button>
                </div>
            </form>
            <div class="text-center text-xs text-gray-500 mt-6">
                <p>账号: zhangsan</p>
                <p>密码: 123</p>
            </div>
        </div>
    </div>
</div>
</body>
<script defer src="assets/js/tailwindcss.js"></script>
<script defer src="assets/js/sweetalert2.js"></script>
<script defer src="assets/js/alpinejs.js"></script>
<script>
    function loginForm() {
        return {
            username: "",
            password: "",
            userList: "",
            async submit() {
                try {
                    const response = await fetch("/api/login", {
                        method: "POST",
                        headers: {
                            "Content-Type": "application/json",
                            "accept": "application/json",
                        },
                        body: JSON.stringify({
                            username: this.username,
                            password: this.password,
                        }),
                    });
                    if (!response.ok) {
                        const data = await response.json();
                        throw new Error(`${data.error.brief}`);
                    }
                    window.location.href = "/users";
                } catch (error) {
                    Swal.fire({
                        title: "Error!",
                        text: error.message,
                        icon: "error",
                        confirmButtonText: "OK",
                    });
                }
            },
        };
    }
</script>
</html>
